package com.yfh.study.manager.controller;

import com.alibaba.fastjson.JSONObject;


import com.yfh.data.base.r.Codes;
import com.yfh.data.base.r.R;
import com.yfh.data.base.r.Rs;
import com.yfh.data.base.utils.QfBeanUtils;
import com.yfh.data.system.dataManager.vo.input.LoginInput;
import com.yfh.data.system.dataManager.vo.output.CurrentSkyOutput;
import com.yfh.data.system.dataManager.vo.output.LoginOutput;
import com.yfh.data.system.dataManager.vo.output.SkyLineOutput;
import com.yfh.data.system.entity.Users;
import com.yfh.study.manager.entity.Sky;
import com.yfh.study.manager.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/test")
    public R testConnection(){
        return Rs.create(Codes.SUCC);
    }

    /**
     * 登录校验
     * @param loginInput
     * @return
     */
    @RequestMapping("/check")
    public R login(LoginInput loginInput){
         String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info("【登录信息】 -{} ,登录时间】 -{}",loginInput, format );
        Users users = QfBeanUtils.beanCopy(loginInput, Users.class);
        Users result = usersService.loginCheck(users);
        if (result ==null) {
            return Rs.createFail(Codes.LOGIN_FAIL);
        }
        LoginOutput loginOutput = QfBeanUtils.beanCopy(result, LoginOutput.class);
        loginOutput.setLoginTime(format);
        return Rs.create(loginOutput);
    }

    /**
     * 获取今天的天气
     * @return
     */
    @RequestMapping("/getTodaySky")
    public R getTodaySky(){
        String weatherUrl="http://wthrcdn.etouch.cn/weather_mini?city=深圳";
        // restTemplate请求目标url并得到响应内容
        ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
        // 响应的正文内容
        String weatherBody = weatherEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(weatherBody);
        //今天后面5天气温
        Object o = jsonObject.getJSONObject("data").get("forecast");
        //当前温度
        String wendu = jsonObject.getJSONObject("data").get("wendu").toString();
        List<Sky> skies = JSONObject.parseArray(o.toString(), Sky.class);

        //处理输出结果
        CurrentSkyOutput currentSkyOutput = QfBeanUtils.beanCopy(skies.get(0), CurrentSkyOutput.class);
        String currentDate = currentSkyOutput.getDate().split("星期")[1];
        String fengli = currentSkyOutput.getFengli().substring(9).split("]]")[0];
        currentSkyOutput.setFengli(fengli);
        currentSkyOutput.setWendu(wendu);
        currentSkyOutput.setDate(currentDate);
        return Rs.create(currentSkyOutput);
    }

    /**
     * 获取近5日的天气，并返回折线图
     * @return
     */
    @RequestMapping("/getSkyLine")
    public R getSkyLine(){
        String weatherUrl="http://wthrcdn.etouch.cn/weather_mini?city=深圳";
        // restTemplate请求目标url并得到响应内容
        ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
        // 响应的正文内容
        String weatherBody = weatherEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(weatherBody);
        //今天后面5天气温
        Object o = jsonObject.getJSONObject("data").get("forecast");
        List<String> xAxis= new ArrayList<>();
        List<Integer> y1High= new ArrayList<>();
        List<Integer> y2Low= new ArrayList<>();
        List<Sky> skies = JSONObject.parseArray(o.toString(), Sky.class).stream().map(
               o1 ->{
                   xAxis.add(o1.getDate().split("星期")[0]);
                  String s = o1.getHigh().substring(3).split("℃")[0];
                   y1High.add(Integer.valueOf(s));
                   String l = o1.getLow().substring(3).split("℃")[0];
                   y2Low.add(Integer.valueOf(l));
                   return o1;
               }
        ).collect(Collectors.toList());

        //处理输出结果
        SkyLineOutput result=new SkyLineOutput();
        result.setXAxis(xAxis);
        result.setY1High(y1High);
        result.setY2Low(y2Low);
        return Rs.create(result);
    }



}
